package com.example.studentalarm.config;

import android.content.Context;
import android.content.SharedPreferences;

import com.example.studentalarm.bean.AlarmConfigBean;

/***
 * 配置文件
 */
public class SharedPreferencesUtils {
    private SharedPreferences mShared;
    private SharedPreferences.Editor mEditor;
    private static SharedPreferencesUtils preferencesUtils;
    private Context context;

    public static SharedPreferencesUtils getInstance(Context context) {
        if (preferencesUtils == null) {
            synchronized (SharedPreferencesUtils.class) {
                if (preferencesUtils == null) {
                    preferencesUtils = new SharedPreferencesUtils(context.getApplicationContext());
                }
            }
        }
        return preferencesUtils;
    }

    public SharedPreferencesUtils(Context context) {
        this.context = context.getApplicationContext();
        initSharedPreference();
    }

    public void initSharedPreference() {
        mShared = context.getSharedPreferences("setConfig", Context.MODE_PRIVATE);
        mEditor = mShared.edit();
    }

    public void saveOpenMusic(boolean isOpenMusic) {
        mEditor.putBoolean("isOpenMusic", isOpenMusic);
        mEditor.commit();
    }

    public void saveOpenVibration(boolean isOpenVibration) {
        mEditor.putBoolean("isOpenVibration", isOpenVibration);
        mEditor.commit();
    }

    public void saveSelectDate(String date){
        mEditor.putString("selectDate", date);
        mEditor.commit();
    }

    /***
     * 获取提醒配置实体
     * @return
     */
    public synchronized AlarmConfigBean getAlarmConfigBean() {
        AlarmConfigBean configBean = new AlarmConfigBean();
        configBean.setMusic(mShared.getBoolean("isOpenMusic", true));
        configBean.setVibration(mShared.getBoolean("isOpenVibration", true));
        return configBean;
    }

}
